package com.anjian.bamin.anjiandianzimap.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.anjian.bamin.anjiandianzimap.R;
import com.anjian.bamin.anjiandianzimap.model.CompanyInfo;

import java.util.List;

public class MySearchAdapter extends BaseAdapter {
    private final Context mContext;
    private final List<CompanyInfo> mCompanyInfoListSearch;
    private LayoutInflater inflater;
    private OnCompanyClickListener onCompanyClickListener;

    public MySearchAdapter(Context context, List<CompanyInfo> mCompanyInfoListSearch){

        mContext = context;

        this.mCompanyInfoListSearch = mCompanyInfoListSearch;
        this.inflater = LayoutInflater.from(mContext);

    }
    @Override
    public int getCount() {
        return mCompanyInfoListSearch.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        ViewHolder holder;
        if (view == null) {
            view = inflater.inflate(R.layout.item_search, parent, false);
            holder = new ViewHolder();
            holder.name = (TextView) view.findViewById(R.id.textView_companyName);
            holder.address = (TextView) view.findViewById(R.id.textView_Address);
            holder.lineCompany = view.findViewById(R.id.line_company);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        final CompanyInfo companyInfo = mCompanyInfoListSearch.get(position);
        final String city = companyInfo.getCompanyName();
        String companyAddress = companyInfo.getCompanyAddress();
        holder.name.setText(city);
        holder.address.setText(companyAddress);
        holder.lineCompany.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onCompanyClickListener != null) {
                    onCompanyClickListener.onCompanyClick(companyInfo);
                }
            }
        });
        return view;
    }
    private static class ViewHolder {
        TextView name;
        TextView address;
        View lineCompany;
    }
    public void setOnCompanyClickListener(OnCompanyClickListener listener) {
        this.onCompanyClickListener = listener;
    }

    public interface OnCompanyClickListener {
        void onCompanyClick(CompanyInfo companyInfo);
    }
}
